package hw3.proxy.gui;

import hw3.proxy.core.ConfigurationProperties;
import hw3.proxy.core.ProxyServer;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import static hw3.proxy.core.ConfigurationProperties.*;


public class ProxyServerGUI
{
	private JTextField m_queryTextField;
	/**
	 * Create the application
	 */
	public ProxyServerGUI()
	{
		m_ProxyServer = new ProxyServer();
		createContents();
	}

	/**
	 * Launch the application
	 * @param args
	 */
	public static void main(String args[])
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					ProxyServerGUI window = new ProxyServerGUI();
					window.m_MainFrame.setVisible(true);
				} 
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Initialize the contents of the frame
	 */
	private void createContents()
	{
		try 
		{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch(Exception e) { }
		m_MainFrame = new JFrame();
		m_MainFrame.setTitle("Dubi Shameni Proxy Server");
		m_MainFrame.getContentPane().setLayout(null);
		m_MainFrame.setName("Proxy Server Console");
		m_MainFrame.setBounds(100, 100, 500, 414);
		m_MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		final JButton startProxyButton = new JButton();
		startProxyButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent arg0)
			{
				try
				{
					new Thread(m_ProxyServer).start();
				}
				catch(Exception e)
				{
					m_ProxyServer = new ProxyServer();
				}
				startProxyButton.setText("Proxy Started !");
				startProxyButton.setEnabled(false);
			}
		});
		startProxyButton.setText("Start Proxy Server");
		startProxyButton.setBounds(30, 30, 185, 26);
		m_MainFrame.getContentPane().add(startProxyButton);

		final JButton constructCacheTablesButton = new JButton();
		constructCacheTablesButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				m_ProxyServer.constructCacheTables();
			}
		});
		constructCacheTablesButton.setText("Construct Cache Tables");
		constructCacheTablesButton.setBounds(30, 116, 185, 26);
		m_MainFrame.getContentPane().add(constructCacheTablesButton);

		final JButton deleteCacheTablesButton = new JButton();
		deleteCacheTablesButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				m_ProxyServer.deleteCacheTables();
			}
		});
		deleteCacheTablesButton.setText("Delete Cache Tables");
		deleteCacheTablesButton.setBounds(30, 166, 185, 26);
		m_MainFrame.getContentPane().add(deleteCacheTablesButton);

		final JButton clearCacheButton = new JButton();
		clearCacheButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				m_ProxyServer.clearCache();
			}
		});
		clearCacheButton.setText("Clear Cache");
		clearCacheButton.setBounds(30, 214, 185, 26);
		m_MainFrame.getContentPane().add(clearCacheButton);

		final JButton terminateProxyServerButton = new JButton();
		terminateProxyServerButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				m_ProxyServer.terminate();
			}
		});
		terminateProxyServerButton.setText("Terminate Proxy Server");
		terminateProxyServerButton.setBounds(30, 72, 185, 26);
		m_MainFrame.getContentPane().add(terminateProxyServerButton);

		m_queryTextField = new JTextField();
		m_queryTextField.setText("SELECT * FROM Cache");
		m_queryTextField.setHorizontalAlignment(SwingConstants.LEFT);
		m_queryTextField.setBounds(246, 75, 219, 165);
		m_MainFrame.getContentPane().add(m_queryTextField);

		final JButton poseQueryButton = new JButton();
		poseQueryButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				String query = m_queryTextField.getText();
				if (query.isEmpty())
				{
					return;
				}
				m_ProxyServer.poseQuery(query);
			}
		});
		poseQueryButton.setText("Pose Query:");
		poseQueryButton.setBounds(246, 30, 219, 26);
		m_MainFrame.getContentPane().add(poseQueryButton);

		final JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Properties", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel.setLayout(null);
		panel.setBounds(11, 252, 471, 118);
		m_MainFrame.getContentPane().add(panel);

		final JCheckBox replaceTokensCheckBox = new JCheckBox();
		replaceTokensCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.REPLACE_TOKENS = replaceTokensCheckBox.isSelected();
			}
		});
		replaceTokensCheckBox.setText("Replace Tokens");
		replaceTokensCheckBox.setBounds(10, 21, 188, 18);
		panel.add(replaceTokensCheckBox);

		final JCheckBox onlyAllowTextualCheckBox = new JCheckBox();
		onlyAllowTextualCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.ONLY_ALLOW_TEXTUAL_RESPONSES = onlyAllowTextualCheckBox.isSelected();
			}
		});
		onlyAllowTextualCheckBox.setText("Only Allow Textual Responses");
		onlyAllowTextualCheckBox.setBounds(10, 45, 188, 18);
		panel.add(onlyAllowTextualCheckBox);

		final JCheckBox outputToConsoleCheckBox = new JCheckBox();
		outputToConsoleCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.OUTPUT_TO_CONSOLE = outputToConsoleCheckBox.isSelected();
			}
		});
		outputToConsoleCheckBox.setText("Output to Console");
		outputToConsoleCheckBox.setBounds(224, 21, 128, 18);
		panel.add(outputToConsoleCheckBox);

		final JCheckBox outputToFrameCheckBox = new JCheckBox();
		outputToFrameCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.OUTPUT_TO_FRAME = outputToFrameCheckBox.isSelected();
			}
		});
		outputToFrameCheckBox.setText("Output to frame");
		outputToFrameCheckBox.setBounds(224, 45, 128, 18);
		panel.add(outputToFrameCheckBox);

		final JCheckBox outputToXhtmlCheckBox = new JCheckBox();
		outputToXhtmlCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.OUTPUT_TO_XHTML_FILE = outputToXhtmlCheckBox.isSelected();
			}
		});
		outputToXhtmlCheckBox.setText("Output to XHTML");
		outputToXhtmlCheckBox.setBounds(224, 69, 128, 18);
		panel.add(outputToXhtmlCheckBox);

		final JCheckBox autoOpenWebCheckBox = new JCheckBox();
		autoOpenWebCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e)
			{
				ConfigurationProperties.AUTO_OPEN_BROWSER = autoOpenWebCheckBox.isSelected();
			}
		});
		autoOpenWebCheckBox.setText("Auto open web browser on result");
		autoOpenWebCheckBox.setBounds(10, 69, 188, 18);
		panel.add(autoOpenWebCheckBox);

		final JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Dubi", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
		panel_1.setLayout(null);
		panel_1.setBounds(8, 13, 232, 233);
		m_MainFrame.getContentPane().add(panel_1);
		
		replaceTokensCheckBox.setSelected(REPLACE_TOKENS);
		onlyAllowTextualCheckBox.setSelected(ONLY_ALLOW_TEXTUAL_RESPONSES);
		outputToConsoleCheckBox.setSelected(OUTPUT_TO_CONSOLE);
		outputToFrameCheckBox.setSelected(OUTPUT_TO_FRAME);
		outputToXhtmlCheckBox.setSelected(OUTPUT_TO_XHTML_FILE);
		autoOpenWebCheckBox.setSelected(AUTO_OPEN_BROWSER);
		
	}

	private JFrame m_MainFrame;
	private ProxyServer m_ProxyServer;
	
}